package com.chuanjiabao.wisdom.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.listener.OnDataSelectListener;
import com.chuanjiabao.wisdom.util.dialog.BaseDialog;

import java.util.Calendar;

import cn.carbs.android.gregorianlunarcalendar.library.data.ChineseCalendar;
import cn.carbs.android.gregorianlunarcalendar.library.view.GregorianLunarCalendarView;
import cn.carbs.android.indicatorview.library.IndicatorView;

public class DialogGLC extends BaseDialog implements View.OnClickListener, IndicatorView.OnIndicatorChangedListener {

    private Context mContext;
    private IndicatorView mIndicatorView;
    private GregorianLunarCalendarView mGLCView;
    private TextView mButtonGetData;
    private int selectIndex;
    private boolean isGregorian;
    private OnDataSelectListener listener;

    public void setOnDataSelectListener(OnDataSelectListener listener) {
        this.listener = listener;
    }

    public DialogGLC(Context context) {
        this(context, true);
    }

    public DialogGLC(Context context, boolean isGregorian) {
        super(context);
        mContext = context;
        this.isGregorian = isGregorian;
        selectIndex = isGregorian ? 0 : 1;
        setContentView(isGregorian ? R.layout.dialog_glc : R.layout.dialog_glc2);
        initWindow();
        mGLCView = this.findViewById(R.id.calendar_view);
        mIndicatorView = this.findViewById(R.id.indicator_view);
        mIndicatorView.setOnIndicatorChangedListener(this);
        mButtonGetData = this.findViewById(R.id.button_get_data);
        mButtonGetData.setOnClickListener(this);
    }

    public void initCalendar() {
        mGLCView.init();
    }

    public void initCalendar(Calendar calendar) {
        mGLCView.init(calendar, isGregorian);
    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_get_data:
                GregorianLunarCalendarView.CalendarData calendarData = mGLCView.getCalendarData();
                Calendar calendar = calendarData.getCalendar();

                String month, day, chineseMonth, chineseDay;
                if ((calendar.get(Calendar.MONTH) + 1) < 10) {
                    month = "0" + (calendar.get(Calendar.MONTH) + 1);
                } else {
                    month = (calendar.get(Calendar.MONTH) + 1) + "";
                }

                if (calendar.get(Calendar.DAY_OF_MONTH) < 10) {
                    day = "0" + calendar.get(Calendar.DAY_OF_MONTH);
                } else {
                    day = calendar.get(Calendar.DAY_OF_MONTH) + "";
                }

                String yangData = calendar.get(Calendar.YEAR) + "-"
                        + month + "-"
                        + day;


                if (calendar.get(ChineseCalendar.CHINESE_MONTH) < 10) {
                    chineseMonth = "0" + calendar.get(ChineseCalendar.CHINESE_MONTH);
                } else {
                    chineseMonth = calendar.get(ChineseCalendar.CHINESE_MONTH) + "";
                }

                if (calendar.get(ChineseCalendar.CHINESE_DATE) < 10) {
                    chineseDay = "0" + calendar.get(ChineseCalendar.CHINESE_DATE);
                } else {
                    chineseDay = calendar.get(ChineseCalendar.CHINESE_DATE) + "";
                }

                @SuppressLint("WrongConstant")
                String yinData = calendar.get(ChineseCalendar.CHINESE_YEAR) + "-"
                        + chineseMonth + "-"
                        + chineseDay;
                if (listener != null) {
                    listener.setOnDataSelectListener(yangData, yinData, selectIndex);
                    dismiss();
                }
                break;
        }
    }

    @Override
    public void onIndicatorChanged(int oldSelectedIndex, int newSelectedIndex) {
        selectIndex = newSelectedIndex;
        if (newSelectedIndex == 0) {
            toGregorianMode();
        } else if (newSelectedIndex == 1) {
            toLunarMode();
        }
    }

    private void toGregorianMode() {
        mGLCView.toGregorianMode();
    }

    private void toLunarMode() {
        mGLCView.toLunarMode();
    }

    private void initWindow() {
        Window win = this.getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        if (lp.width > dp2px(getContext(), 480)) {
            lp.width = dp2px(getContext(), 480);
        }
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        win.setAttributes(lp);
    }

    private int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }

    private int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

}
